package vocoders;

import java.util.Arrays;

//import java.util.*;

public class ArrayUtils {

	public static double[] getIndexes(double[] data, int[] indexes) {
		double[] result = new double[indexes.length];
		for (int i = 0; i < result.length; i++)
			result[i] = data[indexes[i]];
		return result;
	}

	public static int[] getNonZeroIndexes(double[] data) {
		int[] result = new int[data.length];
		int nof_nz_elems = 0;
		for (int i = 0; i < data.length; i++) {
			if (data[i] != 0) {
				result[nof_nz_elems] = i;
				nof_nz_elems++;
			}
		}
		result = Arrays.copyOfRange(result, 0, nof_nz_elems - 1);
		return result;
	}

	public static double[] getMaskedData(double[] data, double[] mask) {
		return getIndexes(data, getNonZeroIndexes(mask));
	}
	
	public static double[] not(double[] data) {
		double[] result=new double[data.length];
		for(int i=0;i<data.length;i++)
			result[i]=data[i]==0?1:0;
		return result;
	}
	
	public static double[] compareEq(double[] a, double[] b) {
		int len=Math.min(a.length, b.length);
		double[] result = new double[Math.max(a.length,b.length)];
		for(int i=0;i<len;i++)
			result[i]=(a[i]==b[i])?1:0;
		return result;
	}
	
	public static double[] compareNotEq(double[] a,double[] b) {
		int len=Math.min(a.length, b.length);
		double[] result=new double[Math.max(a.length,b.length)];
		for(int i=0;i<len;i++)
			result[i]=(a[i]!=b[i])?1:0;
		return result;
	}
	
	public static double[] compareLess(double[] a,double[] b) {
		int len=Math.min(a.length, b.length);
		double[] result=new double[Math.max(a.length,b.length)];
		for(int i=0;i<len;i++)
			result[i]=(a[i]<b[i])?1:0;
		return result;
	}
	
	public static double[] compareGreat(double[] a,double[] b) {
		int len=Math.min(a.length, b.length);
		double[] result=new double[Math.max(a.length,b.length)];
		for(int i=0;i<len;i++)
			result[i]=(a[i]>b[i])?1:0;
		return result;
	}
	
	

	public static void shiftLeft(double[] data, int npos) {
		if (npos > data.length) {
			return;
		}
		for (int i = 0; i < data.length - npos; i++)
			data[i] = data[i + npos];
		for (int i = data.length - npos; i < data.length; i++)
			data[i] = 0;
	}
	
	public static void shiftRight(double[] data, int npos) {
		if (npos > data.length) {
			return;
		}
		for (int i = data.length - 1; i >= npos; i--)
			data[i] = data[i - npos];
		for (int i = 0; i < npos; i++)
			data[i] = 0;
	}

	public static double[] add(double a[], double b[]) {
		double[] result = new double[Math.max(a.length, b.length)];
		for (int i = 0; i < result.length; i++)
			result[i] = a[i] + b[i];
		return result;
	}

	public static double[] add_scalar(double a[], double b) {
		double[] result = new double[a.length];
		for (int i = 0; i < result.length; i++)
			result[i] = a[i] + b;
		return result;
	}

	public static double[] sub(double a[], double b[]) {
		double[] result = new double[Math.max(a.length, b.length)];
		for (int i = 0; i < result.length; i++)
			result[i] = a[i] - b[i];
		return result;
	}

	public static double[] sub_scalar(double a[], double b) {
		double[] result = new double[a.length];
		for (int i = 0; i < result.length; i++)
			result[i] = a[i] - b;
		return result;
	}

	public static double[] mul(double a[], double b[]) {
		double[] result = new double[Math.max(a.length, b.length)];
		for (int i = 0; i < result.length; i++)
			result[i] = a[i] * b[i];
		return result;
	}

	public static double[] mul_scalar(double a[], double b) {
		double[] result = new double[a.length];
		for (int i = 0; i < result.length; i++)
			result[i] = a[i] * b;
		return result;
	}

	public static double[] div(double a[], double b[]) {
		double[] result = new double[Math.max(a.length, b.length)];
		for (int i = 0; i < result.length; i++)
			result[i] = a[i] / b[i];
		return result;
	}

	public static double[] div_scalar(double a[], double b) {
		double[] result = new double[a.length];
		for (int i = 0; i < result.length; i++)
			result[i] = a[i] / b;
		return result;
	}

	public static double sum(double a[]) {
		double result = 0;
		for (int i = 0; i < a.length; i++)
			result += a[i];
		return result;
	}

	public static double prod(double a[]) {
		double result = 1;
		for (int i = 0; i < a.length; i++)
			result *= a[i];
		return result;
	}

	public static double dot_prod(double[] a, double[] b) {
		return sum(mul(a, b));
	}

	public static double[] range(double first, double step, double last) {
		double[] result;
		double count = (last - first) / step;

		if (count < 1)
			return new double[] { first, last };
		else if (count >= 1 && count < 2)
			return new double[] { first, step, last };

		int icount = (int) Math.floor(count);
		result = new double[icount + 1];
		result[0] = first;
		result[result.length - 1] = last;

		for (int i = 1; i < icount; i++)
			result[i] = result[i - 1] + step;

		return result;
	}

	// public static void main(String[] args) {
	// double a[]=range(1,0.7,10);
	// // shiftRight(a,3);
	// System.out.println(Arrays.toString(a));
	// }

}
